package com.ef.web.action;

import java.io.PrintWriter;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.ef.constant.ConfirmMessage;
import com.ef.domain.AdChannel;
import com.ef.domain.MarketingPolicy;
import com.ef.domain.Team;
import com.ef.domain.User;
import com.ef.service.AdChannelService;
import com.ef.service.MarketingPolicyService;
import com.ef.util.CustomExample;
import com.ef.util.PinyinUtil;

public class AdChannelAction extends BaseAction<AdChannel> {

	private static final long serialVersionUID = 1L;
	private AdChannelService adChannelService;

	private MarketingPolicyService marketingPolicyService;

	private Map<String, String> marketingPolicyList;

	protected final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
			.getLog(AdChannelAction.class);

	@SuppressWarnings("serial")
	public String list() {
		this.listResult = adChannelService.findByExample(
				new CustomExample<AdChannel>(this.getModel()) {
					public void appendCondition(Criteria criteria) {
						User user = getSessionUser();
						if (user.getRole() != null && user.getRole().isMK()) {
							if (user.getTeam() != null) {
								criteria.add(Restrictions.eq("team", user
										.getTeam()));
							}
						}
						this.enableLike(MatchMode.ANYWHERE);
					}
				}, this.getStartIndex(), this.getPageSize());
		return LIST;
	}

	public String edit() {

		marketingPolicyList = getMarketingPolicyData();
		return EDIT;
	}

	public Map<String, String> getMarketingPolicyData() {
		Map<String, String> map = new LinkedHashMap<String, String>();
		User user = this.getSessionUser();
		List<MarketingPolicy> list = null;
		if (user.getRole() != null && user.getRole().isMK()) {
			if (user.getTeam() != null) {
				list = marketingPolicyService.findAllByTeam(user.getTeam());
			} else {
				list = marketingPolicyService.findAll();
			}
		} else {
			list = marketingPolicyService.findAll();
		}
		for (MarketingPolicy m : list) {
			map.put(m.getId().toString(), m.getName());
		}
		return map;
	}

	public String save() {
		AdChannel adChannel = this.getModel();
		if (adChannel.getName() != null) {
			adChannel
					.setPyname(PinyinUtil.getChineseSpell(adChannel.getName()));
			String name = adChannel.getName();
			User user = this.getSessionUser();
			if (user.getRole() != null && user.getRole().isMK()) {
				if (user.getTeam() != null) {
					String ac = user.getTeam().getAdultOrChild();
					if (ac.length() > 0 && name.trim().indexOf(ac) < 0) {
						adChannel.setName(name + ac);
						adChannel.setTeam(user.getTeam());
					}
				}
			}
		}
		adChannel.setUpdateTime(new Date());
		adChannel.setUpdateUser(getSessionUser());
		adChannelService.saveOrUpdate(adChannel);
		this.setResult(ConfirmMessage.ADCHANNEL);
		marketingPolicyList = getMarketingPolicyData();
		return EDIT;
	}

	public String delete() {
		String ids = getRequest().getParameter("ids");
		String[] id = ids.split(",");
		for (String i : id) {
			adChannelService.delete(adChannelService.findById(Long.valueOf(i)));
		}

		return DELETE;
	}

	public String autoCompleteAjax() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/plain;charset=GBK ");
		PrintWriter out = null;
		try {
			String q = request.getParameter("q");
			String limit = request.getParameter("limit");
			User user = getSessionUser();
			List<AdChannel> list = null;
			if (user.getRole() != null && user.getRole().isMK()) {
				if (user.getTeam() != null) {
					list = adChannelService.findByAnythingWithTeam(user.getTeam(), q,
							Integer.valueOf(limit));
				} else {
					list = adChannelService.findByAnything(q, Integer.valueOf(limit));
				}
			} else {
				list = adChannelService.findByAnything(q, Integer
						.valueOf(limit));
			}
			// {id:id,name:name}
			String responseStr = "";
			// System.out.println(responseStr);
			out = response.getWriter();
			for (AdChannel key : list) {
				responseStr = ("{id:") + (key.getId()) + (",name:\"")
						+ (key.getName()) + ("\",pyname:\"")
						+ (key.getPyname()) + ("\"}");
				// System.out.println(responseStr);
				out.println(responseStr);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (Exception e) {

				}
			}
		}
		return null;
	}

	public MarketingPolicyService getMarketingPolicyService() {
		return marketingPolicyService;
	}

	public void setMarketingPolicyService(
			MarketingPolicyService marketingPolicyService) {
		this.marketingPolicyService = marketingPolicyService;
	}

	public Map<String, String> getMarketingPolicyList() {
		return marketingPolicyList;
	}

	public void setMarketingPolicyList(Map<String, String> marketingPolicyList) {
		this.marketingPolicyList = marketingPolicyList;
	}

	public AdChannelService getAdChannelService() {
		return adChannelService;
	}

	public void setAdChannelService(AdChannelService adChannelService) {
		this.adChannelService = adChannelService;
	}

}
